package com.example.demo.controllers;

import com.example.demo.entity.GLY;
import com.example.demo.services.GLYService;
import com.example.demo.services.GLYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/gly")
@Controller
public class GLYController {
    @Autowired
    private GLYService glyService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加管理员
     */
    @RequestMapping("/save")
    public String save(@RequestBody GLY gly, Model model){
        try {
            gly = glyService.save(gly);
            model.addAttribute("msg","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","添加失败");
        }
        return "gly.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改管理员
     */
    @RequestMapping("/update")
    public String update(@RequestBody GLY gly, Model model){
        try {
            gly = glyService.update(gly);
            model.addAttribute("msg","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","修改失败");
        }
        return "gly.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除管理员
     */
    @RequestMapping("/delete")
    public String delete(int id, Model model){
        try {
            glyService.delete(id);
            model.addAttribute("msg","删除成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg", "删除失败");
        }
        return "gly.html";
    }
}
